/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ia_0.data_model.character;

import ia_0.data_model.map.World;
import java.awt.Point;
import java.util.HashMap;

/**
 *
 * @author jderenty
 */
public class Move {


    private Point dir;
    private HashMap<Point,Boolean> tried=new HashMap<Point,Boolean>();
    
     public boolean moveTo(Character c,Point p) {

         
         tried.put(World.elements.get(c),true);
         int posX=World.elements.get(c).x;
         int posY=World.elements.get(c).y;
         System.out.println("veut aller"+p.x +" "+p.y);

         //calcul nouvelle direction
         if (dir==null){
             dir=new Point(0,0);
            if(posX<p.x){
                dir.x++;
            } else if(posX>p.x){
                dir.x--;
            }
            if(posY<p.y){
                dir.y++;
            } else if(posY>p.y){
                dir.y--;
            }
         }

         int newX=posX+dir.x;
         int newY=posY+dir.y;
         System.out.println("esaie "+newX+ " "+newY);
         Point newP = new Point(newX,newY);
         //déja essayé ?
         if(tried.containsKey(newP)){
             System.out.println("déja essayé !");
             autreDir();
         } else {

            //déplacement
            if (World.move(c, newP)){
                dir=null;
                cpt=0;
                tried.put(newP,true);
                if(newX==p.x && newY==p.y){
                    return true;
                }
            } else{
                autreDir();
            }

        }
         return false;
     }


     int cpt=0;
    private void autreDir() {
        int[] liste={1,6,3,4,5,2,7};
        if(cpt>6){
            cpt=0;
        }
        rotate(liste[cpt]);
        cpt++;
    }

     private void rotate(int nb){
         for(int i=0;i<nb;i++){
        if(dir.x==1 && (dir.y==0 || dir.y==1)){
            dir.y--;
        } else if(dir.y==-1 && (dir.x==0 || dir.x==1)){
            dir.x--;
        } else if(dir.x==-1 && (dir.y==0 || dir.y==-1)){
            dir.y++;
        } else if(dir.y==1 && (dir.x==0 || dir.x==-1)){
            dir.x++;
        }
         }
     }


}
